// 导出公共方法
import QQMapWX from "./qqmap-wx-jssdk.js";
export default {
	// 优惠券
	dataTypeyhq: function(num) { //时间转格式
		var dd = new Date(num);
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var h = dd.getHours();
		var i = dd.getMinutes();
		var s = dd.getSeconds();
		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;
		return y + '.' + m + '.' + d
		// y + '年' + m + '月' + d + '日'
	},
	//获取addDayCount天后的日期
	getDateStr: function(addDayCount) {
		var dd = new Date();
		dd.setDate(dd.getDate() + addDayCount); //获取AddDayCount天后的日期
		var yyyy = dd.getFullYear();
		var mm = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期，不足10补0
		var dd = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号，不足10补0
		return yyyy + "-" + mm + "-" + dd;
	},
	// 血压计转base
	buf2hex: function(buffer) {
		return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('')
	},
	// 血压计转base
	buf2string: function(buffer) {
		var arr = Array.prototype.map.call(new Uint8Array(buffer), x => x)
		var str = ''
		for (var i = 0; i < arr.length; i++) {
			str += String.fromCharCode(arr[i])
		}
		return str
	},
	// 当前时间减 多少天
	getBeforeDate: function(n) {
		// var n = n;
		var d = new Date();
		var year = d.getFullYear();
		var mon = d.getMonth() + 1;
		var day = d.getDate();
		if (day <= n) {
			if (mon > 1) {
				mon = mon - 1;
			} else {
				year = year - 1;
				mon = 12;
			}
		}
		d.setDate(d.getDate() - n);
		year = d.getFullYear();
		mon = d.getMonth() + 1;
		day = d.getDate();
		let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
		return s;
	},
	// 当前时间减 多少天 时分秒
	getBeforeDateS: function(n) {
		// var n = n;
		var d = new Date();
		var year = d.getFullYear();
		var mon = d.getMonth() + 1;
		var day = d.getDate();
		if (day <= n) {
			if (mon > 1) {
				mon = mon - 1;
			} else {
				year = year - 1;
				mon = 12;
			}
		}
		d.setDate(d.getDate() - n);
		year = d.getFullYear();
		mon = d.getMonth() + 1;
		day = d.getDate();
		var h = d.getHours();
		var i = d.getMinutes();
		var ss = d.getSeconds();
		let s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day) + ' ' + h + ':' +
			i + ':' + ss;
		return s;
	},
	// 未登录跳转登录页
	globaltoken: function() {
		var pages = getCurrentPages();
		pages = pages.reverse();
		pages = pages[pages.length - 1].route //当前页面url
		console.log(pages, '登录返回页面')
		uni.setStorageSync('upPageurl', '/' + pages) //上一页路径
		if (!global.token) {
			uni.navigateTo({
				url: '/pages/signIn/signIn'
			})
			return;
		}
	},
	// 获取地理位置信息
	getCityInfo: function() {
		// 腾讯地图Api
		const qqmapsdk = new QQMapWX({
			key: "5VYBZ-HKAKW-Z73RU-RVO7K-W5DM7-GLFGQ"
		});
		// 授权
		uni.getLocation({
			type: "gcj02", //  wgs84: 返回GPS坐标，gcj02: 返回国测局坐标
			success: function(res) {
				console.log(res, 66666666666666666)
				const location = {
					latitude: res.latitude,
					longitude: res.longitude
				};
				qqmapsdk.reverseGeocoder({
					location,
					success: function(locationres) {
						let province = locationres.result.address_component.province, //省
							city = locationres.result.address_component.city, //市
							district = locationres.result.address_component.district; //区
						uni.$emit('locationres', {
							province: province,
							city: city,
							district: district,
						})
					},
					fail: function(res) {
						// console.log(res, 2111);

					},
					complete: function(res) {
						console.log(res);
					}
				});
			},
			fail() {
				uni.$emit('locationres', {
					province: '',
					city: '',
					district: '',
				})
			}
		});
	},
	isJSON: function(str) { //判断是否是json对象
		if (typeof str == 'string') {
			try {
				var obj = JSON.parse(str);
				if (typeof obj == 'object' && obj) {
					return true;
				} else {
					return false;
				}

			} catch (e) {
				console.log('error：' + str + '!!!' + e);
				return false;
			}
		}
		console.log('It is not a string!')
	},
	// 获取当前时间
	thisdayFormat2: function(type, daynum) {
		var dd = new Date();
		if (daynum == 1) { //昨天
			dd.setTime(dd.getTime() - 24 * 60 * 60 * 1000);
		} else if (daynum == 3) {
			dd.setTime(dd.getTime() + 24 * 60 * 60 * 1000);
		} else if (daynum == 4) {
			dd.setTime(dd.getTime() + 48 * 60 * 60 * 1000);
		} else if (daynum == '-2') {
			dd.setTime(dd.getTime() - 48 * 60 * 60 * 1000);
		} else if (daynum == '-1') {
			dd.setTime(dd.getTime() - 23 * 60 * 60 * 1000);
		}
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var h = dd.getHours();
		var i = dd.getMinutes();
		var s = dd.getSeconds();
		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;
		if (type) {
			return y + '年' + m + '月' + d + '日' + ' ' + h + ':' + i + ':' + s
		} else {
			return y + '-' + m + '-' + d
		}
	},
	// 获取当前时间
	thisday: function(data,type) {
		data = data.replace(/-/g, '/')
		var dd = new Date(data);
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var h = dd.getHours();
		var i = dd.getMinutes();
		var s = dd.getSeconds();
		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;
		if (type) {
			return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s
		} else {
			return y + '-' + m + '-' + d
		}
	},
	formatMsgTime: function(timespan, etimestamp) {
		// var dateTime = new Date(timespan) // 将传进来的字符串或者毫秒转为标准时间
		// var year = dateTime.getFullYear()
		// var month = dateTime.getMonth() + 1
		// var day = dateTime.getDate()
		// var hour = dateTime.getHours()
		// var minute = dateTime.getMinutes()
		// // var second = dateTime.getSeconds()
		// var millisecond = dateTime.getTime() // 将当前编辑的时间转换为毫秒
		// var now = new Date(etimestamp) // 获取本机当前的时间
		// var nowNew = now.getTime() // 将本机的时间转换为毫秒
		// var milliseconds = 0
		// var timeSpanStr
		// milliseconds = nowNew - millisecond
		// // if (milliseconds <= 1000 * 60 * 1) { // 小于一分钟展示为刚刚
		// //   timeSpanStr = 1
		// // } else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) { // 大于一分钟小于一小时展示为分钟
		// timeSpanStr = Math.round((milliseconds / (1000 * 60)))
		// // } else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) { // 大于一小时小于一天展示为小时
		// //   timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前'
		// // } else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) { // 大于一天小于十五天展示位天
		// //   timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前'
		// // } else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year === now.getFullYear()) {
		// //   timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute
		// // } else {
		// //   timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute
		// // }
		// return timeSpanStr
		var day_day1 = new Date(timespan);
		var day_day2 = new Date(etimestamp);

		let disparity = day_day2.getTime() - day_day1.getTime();
		// 转为分钟数的时候，可能会出现精度丢失;你需要注意下
		// let min = Math.round(disparity / 1000 / 60);
		// console.log(disparity)
		return parseInt(disparity / 60)
	},
	formatMsgTimePl: function(timespan) {
		var dateTime = new Date(timespan) // 将传进来的字符串或者毫秒转为标准时间
		var year = dateTime.getFullYear()
		var month = dateTime.getMonth() + 1
		var day = dateTime.getDate()
		var hour = dateTime.getHours()
		var minute = dateTime.getMinutes()
		// var second = dateTime.getSeconds()
		var millisecond = dateTime.getTime() // 将当前编辑的时间转换为毫秒
		var now = new Date() // 获取本机当前的时间
		var nowNew = now.getTime() // 将本机的时间转换为毫秒
		var milliseconds = 0
		var timeSpanStr
		milliseconds = nowNew - millisecond
		if (milliseconds <= 1000 * 60 * 1) { // 小于一分钟展示为刚刚
			timeSpanStr = '刚刚'
		} else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) { // 大于一分钟小于一小时展示为分钟
			timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前'
		} else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) { // 大于一小时小于一天展示为小时
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前'
		} else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 *
			15) { // 大于一天小于十五天展示位天
			timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前'
		}
		//   else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year === now.getFullYear()) {
		//    timeSpanStr = month + '-' + day 
		// // + ' ' + hour + ':' + minute
		//  }
		else {
			timeSpanStr = year + '-' + month + '-' + day
			// + ' ' + hour + ':' + minute
		}
		return timeSpanStr
	},
	getToday: function() { // 获取今日
		let date = new Date();
		let m = date.getMonth() + 1,
			d = date.getDate();
		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		return [date.getFullYear(), m, d];
	},
	formatMinutes: function(minutes) { //分钟  转天 时  分

		var day = parseInt(Math.floor(minutes / 1440));

		var hour = day > 0

			?
			Math.floor((minutes - day * 1440) / 60)

			:
			Math.floor(minutes / 60);

		var minute = hour > 0

			?
			Math.floor(minutes - day * 1440 - hour * 60)

			:
			minutes;

		var time = "";

		if (day > 0) time += day + "天";

		if (hour > 0) time += hour + "小时";

		if (minute > 0) time += minute + "分钟";

		return time;

	},
	formatMinutesdata: function(minutes) { //分钟  转天 时  分

		var day = parseInt(Math.floor(minutes / 1440));

		var hour = day > 0

			?
			Math.floor((minutes - day * 1440) / 60)

			:
			Math.floor(minutes / 60);

		var minute = hour > 0

			?
			Math.floor(minutes - day * 1440 - hour * 60)

			:
			minutes;

		var time = "";

		// 	if (day > 0) time += day + "天";

		// 	if (hour > 0) time += hour + "小时";

		// 	if (minute > 0) time += minute + "分钟";

		return {
			day: day,
			hour: hour,
			minute: minute
		};

	},
	formatMinutesHour: function(minutes) { //分钟  转天 时  分
		var hour = Math.floor(minutes / 60);
		return hour
	},
	dataType: function(num) { //时间转格式
		var dd = new Date(num);
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var h = dd.getHours();
		var i = dd.getMinutes();
		var s = dd.getSeconds();
		// m = m < 10 ? '0' + m : m;
		// d = d < 10 ? '0' + d : d;
		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;
		return {
			n: y,
			y: m,
			r: d
		}
		// y + '年' + m + '月' + d + '日'
	},
	dataTypeday: function(num) { //时间转格式
		var dd = new Date(num);
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var h = dd.getHours();
		var i = dd.getMinutes();
		var s = dd.getSeconds();
		m = m < 10 ? '0' + m : m;
		d = d < 10 ? '0' + d : d;
		h = h < 10 ? '0' + h : h;
		i = i < 10 ? '0' + i : i;
		s = s < 10 ? '0' + s : s;
		return {
			n: y,
			y: m,
			r: d,
			h: h,
			i: i,
			s: s
		}
	},


	funFor: function(range, _this, name, company, spot) {
		if (spot === 0) {
			spot = 99999
		}

		function listFun(_this, name, numArr, company, spot) { //name 最小 最大 单位  是否为小数点
			let allArr = []
			for (let i = 0; i < numArr.length; i++) {
				let thisArr = [];
				for (let j = numArr[i][0]; j < numArr[i][1]; j++) {
					if (i == spot) { //
						thisArr.push('.' + j)
					} else {
						thisArr.push(j)
					}
				}
				allArr.push(thisArr)
			}
			allArr.push([company])
			_this.$set(_this, [name], allArr)
		}
		// . -g全选 正则冲突
		range = range.replace(".", "/")
		range = range.replace(".", "/")
		range = range.replace(".", "/")
		// end
		range = range.split('/')
		_this.listLength[name] = range.length
		let numArr = [];
		for (let i = 0; i < range.length; i++) {
			let maxmin = range[i].split('-')
			maxmin[0] = parseFloat(maxmin[0].replace('(', ''))
			maxmin[1] = parseFloat(maxmin[1])
			numArr.push(maxmin)
		}
		console.log(numArr, 4233333)
		listFun(_this, name, numArr, company, spot)
	},
	itemFor: function(_this, name, data, company, spot) { //获取名 数据 是否要单位
		let numB = '';
		let Length = _this.listLength[name]
		if (company) {
			Length = Length + 1
		}
		for (let i = 0; i < Length; i++) {
				numB = numB + '' + data[i]
		}
		console.log(numB)
		return numB
	},
	httpHttps: function(url) { //http 换https
		if (url.indexOf("https") < 0) {
		url = url.replace("http:", "https:")
		}
		return url
	},
}
